/**
 * 获取每月1号为星期几
 * @param year
 * @param month
 * @return {number}
 */
function getFirstDayWeek (year, month) {
    const date = new Date(year, month-1, 1)
    return date.getDay()
}

/**
 * 获取每月月底为星期几
 * @param year
 * @param month
 */
function getLastDayWeek(year, month) {
    const monthDays = getMonthDayCount(year, month)
    const date = new Date(year, month -1, monthDays)
    return date.getDay()
}


/**
 * 获取每月天数
 * @param year
 * @param month
 * @return {number}
 */
function getMonthDayCount(year, month) {
    const date = new Date(year, month, 0);
    return date.getDate();
}

/**
 * 获取当月1号的前几天
 * @param year
 * @param month
 * @return {*[]}
 */
function getLastMonthRestDays(year, month) {
    const weekDays = getFirstDayWeek(year, month); // 获取当前1号为星期几
    let lastDate = getMonthDayCount(year, month-1) // 上个月的最后一天
    const restDays = []
    while (restDays.length < weekDays - 1) {
        restDays.push(lastDate--)
    }
    return restDays.reverse()
}

/**
 * 获取当月最后一天的几天
 * @param year
 * @param month
 * @return {*[]}
 */
function getNextMonthRestDays(year, month) {
    let weekDays = 7 - getLastDayWeek(year, month) // 当月最后一天为星期几
    const restDays = []
    while(weekDays) {
        restDays.push(weekDays--)
    }
    return restDays.reverse()
}

/**
 * 获取日期
 * @param year
 * @param month
 * @param day
 * @return {number}
 */
function getTime (year, month, day) {
    const d = new Date(year, month - 1, day)
    return new Date(d.setHours(0,0, 0)).getTime()
}


/**
 * 日期容器
 * @param year
 * @param month
 * @return {{next: *[], last: *[]}}
 */
function calendarWrap(year, month) {
    const monthDays = getMonthDayCount(year, month)
    const current = new Array(monthDays).fill(null).map((i, idx) => idx+1)
    const res = {
        last: getLastMonthRestDays(year, month),
        current,
        next: getNextMonthRestDays(year, month)
    }

    /**
     * 如果last === 0 也就是当月1号为星期一
     * 则需要在下月天数补满42位
     */
    const curCount = res.last.length + res.current.length + res.next.length
    if (curCount !== 42) {
        let nextAppendLen = 42 - (res.last.length + res.current.length + res.next.length);
        const nestAppendList = []
        const nextLast = res.next[res.next.length - 1]
        while(nextAppendLen > 0) {
            nestAppendList.push(nextLast + nextAppendLen--)
        }
        res.next = res.next.concat(nestAppendList.reverse())
    }
    return res
}

function calendarValueWrap(year, month) {
    const { last, current, next } = calendarWrap(year, month) // 取出上月、当月、下月多少号
    const format = (y, m, d, t) => {
        return {
            value: d,
            valueStr: `${y}-${m}-${d}`,
            wrapMonth: month,
            date: getTime(y, m, d),
            type: t
        }
    }
    const lastValue = last.map(v => format(year, month - 1, v, 'last'))
    const currentValue = current.map(v => format(year, month, v, 'current'))
    const nextValue = next.map(v => format(year, month + 1, v, 'next'))
    return {
        last: lastValue,
        current: currentValue,
        next: nextValue
    }
}

/**
 * 格式化日期
 * @param d
 * @return {string}
 */
function format(d) {
    const date = d ? new Date(d) : new Date()
    const join = d => d < 10 ? '0' + d : d
    const o = {
        "YYYY": date.getFullYear(),
        "MM": join(date.getMonth() + 1), //月份
        "DD": join(date.getDate()), //日
        "hh": join(date.getHours()), //小时
        "mm": join(date.getMinutes()), //分
        "ss": join(date.getSeconds()), //秒
    };
    return [o.YYYY, o.MM, o.DD].join('-')
}

/**
 * 转换日期对象
 * @param d
 * @return {number}
 */
function formatDateObj(d) {
    const date = d ? new Date(d) : new Date()
    const map = {
        year: date.getFullYear(),
        month: date.getMonth() + 1, //月份
        date: date.getDate(), //日
        hours: date.getHours(), //小时
        minute: date.getMinutes(), //分
        second: date.getSeconds() //秒
    }
    return map
}


export { getNextMonthRestDays, calendarWrap, calendarValueWrap, format, formatDateObj }
